/**
 * 神州信息(2013)
 */
package net.nextvisual.report.start;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import net.abbp.swing.layout.LayoutUtils;
import net.abbp.swing.util.GuiUtils;

import com.reportgear.report.print.PaperSize;

/**
 * 参数选择面板
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since 销售宝 2.0
 * 
 *        <pre>
 * 历史：
 *      建立: 2013-8-6 lexloo
 * </pre>
 */
public class SelectParamPane extends JPanel {
    /**
     * UID
     */
    private static final long serialVersionUID = -2028836943993189411L;
    /**
     * 确定按钮返回值
     */
    public static final int RETURN_OK = 1;
    /**
     * 取消按钮返回值
     */
    public static final int RETURN_CANCEL = 0;
    /**
     * 是否分页打印
     */
    private JCheckBox chkPagePring = new JCheckBox();
    /**
     * 分页类型
     */
    private JComboBox cbxPageType = new JComboBox(new String[]{"成本中心", "组织机构"});
    /**
     * 纸张大小
     */
    private JComboBox cbxPageSize = new JComboBox(PaperSize.PAPER_SIZE_ARRAY);
    /**
     * 纸张方向
     */
    private JComboBox cbxPageOrientation = new JComboBox(new String[]{"横向", "纵向"});

    /**
     * 返回值
     */
    private int returnValue = RETURN_CANCEL;

    /**
     * 构造函数
     */
    public SelectParamPane() {
        this.setLayout(new BorderLayout());

        JPanel pnl1 = new JPanel(new GridBagLayout());

        pnl1.add(new JLabel("分页打印", JLabel.RIGHT), LayoutUtils.newGridBagConstraints(0, 0));
        pnl1.add(chkPagePring, LayoutUtils.newGridBagConstraints(1, 0));

        pnl1.add(new JLabel("分页依据", JLabel.RIGHT), LayoutUtils.newGridBagConstraints(0, 1));
        pnl1.add(cbxPageType, LayoutUtils.newGridBagConstraints(1, 1));

        pnl1.add(new JLabel("选择纸张", JLabel.RIGHT), LayoutUtils.newGridBagConstraints(0, 2));
        cbxPageSize.setSelectedItem(PaperSize.PAPERSIZE_A4);
        pnl1.add(cbxPageSize, LayoutUtils.newGridBagConstraints(1, 2));

        pnl1.add(new JLabel("打印方向", JLabel.RIGHT), LayoutUtils.newGridBagConstraints(0, 3));
        pnl1.add(cbxPageOrientation, LayoutUtils.newGridBagConstraints(1, 3));

        this.add(pnl1);

        JPanel pnl2 = new JPanel();
        pnl2.setBorder(BorderFactory.createEtchedBorder());
        JButton btnOK = new JButton("确定");
        btnOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                returnValue = SelectParamPane.RETURN_OK;

                SwingUtilities.getWindowAncestor(SelectParamPane.this).dispose();
            }
        });
        pnl2.add(btnOK);

        JButton btnCancel = new JButton("取消");
        btnCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                returnValue = SelectParamPane.RETURN_CANCEL;
                SwingUtilities.getWindowAncestor(SelectParamPane.this).dispose();
            }
        });
        pnl2.add(btnCancel);

        this.add(pnl2, BorderLayout.SOUTH);
    }

    /**
     * 返回选择参数
     * 
     * @return 选择参数
     */
    public Parameter getParameter() {
        Parameter p = new Parameter();

        p.setTypePaging(chkPagePring.isSelected());
        p.setType(cbxPageType.getSelectedIndex());
        p.setPageSize((PaperSize) cbxPageSize.getSelectedItem());
        p.setOrientation(cbxPageOrientation.getSelectedIndex());

        return p;
    }

    /**
     * @return the returnValue
     */
    public int getReturnValue() {
        return returnValue;
    }

    /**
     * @param returnValue the returnValue to set
     */
    public void setReturnValue(int returnValue) {
        this.returnValue = returnValue;
    }

    /**
     * 在对话框中显示组件
     * 
     * @param parent 父级窗体
     */
    public void showInDialog(JFrame parent) {
        JDialog jd = new JDialog(parent, "选择打印参数");
        jd.setSize(new Dimension(360, 240));
//        jd.setAlwaysOnTop(true);
        jd.setModal(true);
        jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        jd.add(this);
        GuiUtils.centerWindow(jd);

        jd.setVisible(true);
    }

}
